/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:unlocker:0.1-SNAPSHOT",
            "org.helgoboss:scala-additions:0.1-SNAPSHOT",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-api:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-convenience:0.1-SNAPSHOT"
        ],
        "commandletId": "org.helgoboss.unlocker"
    }
*/

package org.helgoboss.unlocker_commandlet

import org.helgoboss.commandlet._
import org.helgoboss.osgi_additions._
import org.helgoboss.osgi_additions.metatype._
import org.helgoboss.unlocker.Unlocker

class Activator extends OsgiProvider with OsgiServiceWatcher {
    whenBundleActive {
        whenServicePresent[Unlocker] {
            new UnlockerCommandlet(_).providesService[Commandlet]
        }
    }
}


class UnlockerCommandlet(unlocker: Unlocker) extends Commandlet {
    val id = "org.helgoboss.unlocker"

    def execute(context: ExecutionContext) = {
        import org.helgoboss.commandlet_convenience.Implicits._
        
        val options = context.arguments.options
        val fileString = (options.qualified.get("file") orElse options.positional.lift(0)) getOrElse context.fail("Please provide a file!")
        val file = context.resolveFile(fileString)
        unlocker.unlock(file)
        0
    }
}